﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.Entity;
using DrCarDrive.Util;
using DrCarDrive.DBService;
using DrCarDrive.Wpf.WpfWindow;
using System.Data;

namespace DrCarDrive.Wpf.WpfPage
{
    /// <summary>
    /// Interaction logic for AddVillageRecordPage.xaml
    /// </summary>


    public partial class Settings : Page
    {
        private DrcardriveDB _context = new DrcardriveDB();
        private static Settings instance;
        private List<tbl_expense_desc> descList = null;
        private tbl_expense_desc selectedExpenseObject = null;
        private List<tbl_drive_medicine> medicineList = null;
        private tbl_drive_medicine selectedMedicineObject = null;
        private List<tbl_car> carList = null;
        private tbl_car selectedCarObject = null;

        public Settings()
        {
            InitializeComponent();
        }
        public static Settings Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new Settings();
                }

                return instance;
            }
        }

        private void Page_Loaded_1(object sender, RoutedEventArgs e)
        {
            loadMedicineList();
            loadCarList();
            loadExpensesList();
            this.Cursor = Cursors.Hand;
        }




        private void Button_Click_2(object sender, RoutedEventArgs e)
        {

        }

        private void txtMedicineName_TextChanged(object sender, TextChangedEventArgs e)
        {

        }
        /* Medicine Settings */

        private void clearMedicineSettings()
        {
            txtMedicineName.Text = "";
            txtMedicineType.Text = "";
            txtMedicineRemark.Text = "";
        }

        private void btnMedicineSettingSave_Click(object sender, RoutedEventArgs e)
        {
            if (validateMedicine() == true)
            {
                tbl_drive_medicine medicine = new tbl_drive_medicine();
                medicine.ID = GUIDGenerator.medicineKey();
                medicine.NAME = txtMedicineName.Text;
                medicine.TYPE = txtMedicineType.Text;
                medicine.REMARK = txtMedicineRemark.Text;

                MedicineService medicineService = new MedicineService();
                if (!medicineService.inDatabase(medicine))
                {
                    int status = medicineService.saveMedicine(medicine);
                    if (status > 0)
                    {
                        MSGWindow success = new MSGWindow(Messages.TTL_MEDICINE, Messages.MSG_MEDICINE_ADD);
                        success.ShowDialog();
                        loadMedicineList();
                        clearMedicineSettings();
                    }
                }
                else
                {
                    MSGWindow error = new MSGWindow(Messages.TTL_MEDICINE, Messages.MSG_MEDICINE_EXISTS);
                    error.ShowDialog();
                }

            }
            else
            {
                MSGWindow errorWindow = new MSGWindow(Messages.TTL_FILL_REQUIRED, Messages.MSG_FILL_REQUIRED_INFORMATION);
                errorWindow.ShowDialog();
            }



        }

        private void loadMedicineList()
        {
            MedicineService medicineService = new MedicineService();
            medicineList = medicineService.viewMedicineList();
            lsvMedicine.Items.Clear();
            foreach (tbl_drive_medicine medicine in medicineList)
            {
                lsvMedicine.Items.Add(new ListViewItem { Content = medicine.NAME });
            }
        }

        private Boolean validateMedicine()
        {
            if (txtMedicineName.Text == "" || txtMedicineType.Text == "")
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        private void btnMedicineUpdate_Click(object sender, RoutedEventArgs e)
        {
            if (validateMedicine() == true)
            {
                //selectedMedicineObject.NAME = txtMedicineName.Text;
                selectedMedicineObject.TYPE = txtMedicineType.Text;
                selectedMedicineObject.REMARK = txtMedicineRemark.Text;

                MedicineService medicineService = new MedicineService();
                int status = medicineService.updateMedicine(selectedMedicineObject);
                if (status > 0)
                {
                    MSGWindow success = new MSGWindow(Messages.TTL_MEDICINE, Messages.MSG_MEDICINE_UPDATE);
                    success.ShowDialog();
                    loadMedicineList();
                }

            }
            else
            {
                MSGWindow errorWindow = new MSGWindow(Messages.TTL_FILL_REQUIRED, Messages.MSG_FILL_REQUIRED_INFORMATION);
                errorWindow.ShowDialog();
            }
        }

        private void btnMedicineDelete_Click(object sender, RoutedEventArgs e)
        {
            ConfirmationWindow messageBoxResult = new ConfirmationWindow(Messages.TTL_MEDICINE, "Are you sure to delete record ?");
            messageBoxResult.ShowDialog();
            if (ConfirmationWindow.isConfirm == MessageBoxResult.Yes)
            {
                MedicineService medicineService = new MedicineService();
                int status = medicineService.remove(selectedMedicineObject);
                if (status > 0)
                {
                    MSGWindow success = new MSGWindow(Messages.TTL_MEDICINE, Messages.MSG_MEDICINE_DELETE);
                    success.ShowDialog();
                    loadMedicineList();
                    clearMedicineSettings();
                }
            }
        }

        private void btnMedicineCancel_Click(object sender, RoutedEventArgs e)
        {
            clearMedicineSettings();
        }


        private void lsvMedicine_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lsvMedicine.SelectedIndex != -1)
            {
                ListViewItem lvi = ((sender as ListView).SelectedItem as ListViewItem);
                foreach (tbl_drive_medicine med in medicineList)
                {
                    if (med.NAME.Equals(lvi.Content.ToString()))
                    {
                        txtMedicineName.Text = med.NAME;
                        txtMedicineType.Text = med.TYPE;
                        txtMedicineRemark.Text = med.REMARK;
                        selectedMedicineObject = med;
                        medicineViewChangeStatus("update");
                    }
                }
            }
            else
            {
                medicineViewChangeStatus("save");
            }
        }

        private void medicineViewChangeStatus(string type)
        {
            if (type.Equals("save"))
            {
                spMedicineDetails.Visibility = System.Windows.Visibility.Visible;
                grdMedicineSave.Visibility = System.Windows.Visibility.Visible;
                grdMedicineUpdate.Visibility = System.Windows.Visibility.Hidden;
                txtMedicineName.IsEnabled = true;
                lsvExpense.SelectedIndex = -1;
                clearMedicineSettings();
            }
            else if (type.Equals("update"))
            {
                spMedicineDetails.Visibility = System.Windows.Visibility.Visible;
                grdMedicineSave.Visibility = System.Windows.Visibility.Hidden;
                grdMedicineUpdate.Visibility = System.Windows.Visibility.Visible;
                txtMedicineName.IsEnabled = false;
            }
        }

        private void btnMedicineAddNew_Click(object sender, RoutedEventArgs e)
        {
            clearMedicineSettings();
            medicineViewChangeStatus("save");
        }

        /* end medicine records */

        /* Expenses Records */
        private void btnExpensesCancel_Click(object sender, RoutedEventArgs e)
        {
            clearExpensesSettings();
        }

        private void clearExpensesSettings()
        {
            txtExpenseRemark.Text = "";
            txtExpenseType.Text = "";
        }

        private void loadExpensesList()
        {
            ExpensesDescriptionService expensesDescriptionService = new ExpensesDescriptionService();
            descList = expensesDescriptionService.viewAll();
            lsvExpense.Items.Clear();
            foreach (tbl_expense_desc desc in descList)
            {
                lsvExpense.Items.Add(new ListViewItem { Content = desc.DESCRIPTION });
            }
        }

        private void btnExpensesSave_Click(object sender, RoutedEventArgs e)
        {

            if (validateExpenses() == true)
            {
                tbl_expense_desc expensesDesc = new tbl_expense_desc();
                expensesDesc.ID = GUIDGenerator.expensesDescKey();
                expensesDesc.DESCRIPTION = txtExpenseType.Text;
                expensesDesc.REMARK = txtExpenseRemark.Text;

                ExpensesDescriptionService expensesDescriptService = new ExpensesDescriptionService();

                if (!expensesDescriptService.inDatabase(expensesDesc))
                {
                    int status = expensesDescriptService.save(expensesDesc);
                    if (status > 0)
                    {
                        MSGWindow success = new MSGWindow(Messages.TTL_EXPENSE_DESC, Messages.MSG_EXPENSE_DESC_ADD);
                        success.ShowDialog();
                        loadExpensesList();
                        clearExpensesSettings();
                    }
                }
                else
                {
                    MSGWindow error = new MSGWindow(Messages.TTL_EXPENSE_DESC, Messages.MSG_EXPENSE_DESC_EXISTS);
                    error.ShowDialog();
                }
            }
            else
            {
                MSGWindow errorWindow = new MSGWindow(Messages.TTL_FILL_REQUIRED, Messages.MSG_FILL_REQUIRED_INFORMATION);
                errorWindow.ShowDialog();
            }



        }

        private Boolean validateExpenses()
        {
            if (txtExpenseType.Text == "")
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        private void btnExpensesAddNew_Click(object sender, RoutedEventArgs e)
        {
            clearExpensesSettings();
            expensesViewChangeStatus("save");
        }

        private void lsvExpense_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // MessageBox.Show(lsvExpense.SelectedIndex.ToString());
            if (lsvExpense.SelectedIndex != -1)
            {
                ListViewItem lvi = ((sender as ListView).SelectedItem as ListViewItem);
                foreach (tbl_expense_desc desc in descList)
                {
                    if (desc.DESCRIPTION.Equals(lvi.Content.ToString()))
                    {
                        txtExpenseType.Text = desc.DESCRIPTION;
                        txtExpenseRemark.Text = desc.REMARK;
                        selectedExpenseObject = desc;
                        expensesViewChangeStatus("update");
                    }
                }
            }
            else
            {
                expensesViewChangeStatus("save");
            }

        }

        private void btnExpensesUpdate_Click(object sender, RoutedEventArgs e)
        {
            if (validateExpenses() == true)
            {
                //selectedExpenseObject.DESCRIPTION = txtExpenseType.Text;
                selectedExpenseObject.REMARK = txtExpenseRemark.Text;

                ExpensesDescriptionService expensesDescriptService = new ExpensesDescriptionService();

                int status = expensesDescriptService.update(selectedExpenseObject);
                if (status > 0)
                {
                    MSGWindow success = new MSGWindow(Messages.TTL_EXPENSE_DESC, Messages.MSG_EXPENSE_DESC_UPDATE);
                    success.ShowDialog();
                    loadExpensesList();
                }
            }
            else
            {
                MSGWindow errorWindow = new MSGWindow(Messages.TTL_FILL_REQUIRED, Messages.MSG_FILL_REQUIRED_INFORMATION);
                errorWindow.ShowDialog();
            }
        }

        private void btnExpensesDelete_Click(object sender, RoutedEventArgs e)
        {
            ConfirmationWindow cfw = new ConfirmationWindow("Delete Record", "Are you sure to delete record ?");
            cfw.ShowDialog();
            MessageBoxResult messageBoxResult = ConfirmationWindow.isConfirm;//System.Windows.MessageBox.Show("Are you sure to delete record ?", Messages.TTL_EXPENSE_DESC, System.Windows.MessageBoxButton.YesNo);
            if (messageBoxResult == MessageBoxResult.Yes)
            {
                ExpensesDescriptionService expensesDescriptService = new ExpensesDescriptionService();
                int status = expensesDescriptService.remove(selectedExpenseObject);
                if (status > 0)
                {
                    MSGWindow success = new MSGWindow(Messages.TTL_EXPENSE_DESC, Messages.MSG_EXPENSE_DESC_DELETE);
                    success.ShowDialog();
                    loadExpensesList();
                    clearExpensesSettings();
                }
            }

        }

        private void expensesViewChangeStatus(string type)
        {
            if (type.Equals("save"))
            {
                spExpensesDetail.Visibility = System.Windows.Visibility.Visible;
                grdExpensesSave.Visibility = System.Windows.Visibility.Visible;
                grdExpenseUpdate.Visibility = System.Windows.Visibility.Hidden;
                txtExpenseType.IsEnabled = true;
                lsvExpense.SelectedIndex = -1;
                clearExpensesSettings();
            }
            else if (type.Equals("update"))
            {
                spExpensesDetail.Visibility = System.Windows.Visibility.Visible;
                grdExpensesSave.Visibility = System.Windows.Visibility.Hidden;
                grdExpenseUpdate.Visibility = System.Windows.Visibility.Visible;
                txtExpenseType.IsEnabled = false;
            }

        }




        /* end expenses records */

        /* car settings */
        private void btnCarSave_Click(object sender, RoutedEventArgs e)
        {
            if (txtPassword.Password != txtConfPassword.Password)
            {
                MSGWindow errorWindow = new MSGWindow(Messages.TTL_PASS_MATCH, Messages.MSG_PASS_MATCH_ERROR);
                errorWindow.ShowDialog();
            }
            else
            {
                if (validateCar() == true)
                {
                    tbl_car car = new tbl_car();
                    car.ID = GUIDGenerator.carKey();
                    car.MODEL = txtCarModel.Text;
                    car.REG_NO = txtCarRegNo.Text;
                    car.REMARK = txtCarRemark.Text;
                    car.USER_NAME = txtCarRegNo.Text;
                    car.PASSWORD = txtPassword.Password;

                    DoctorCarService carService = new DoctorCarService();

                    if (!carService.inDatabase(car))
                    {
                        int status = carService.save(car);
                        if (status > 0)
                        {
                            MSGWindow success = new MSGWindow(Messages.TTL_CAR_ADD, Messages.MSG_CAR_ADD);
                            success.ShowDialog();
                            loadCarList();
                            clearCarSettings();
                        }
                    }
                    else
                    {
                        MSGWindow error = new MSGWindow(Messages.TTL_CAR_ADD, Messages.MSG_CAR_EXISTS);
                        error.ShowDialog();
                    }

                }
                else
                {
                    MSGWindow errorWindow = new MSGWindow(Messages.TTL_FILL_REQUIRED, Messages.MSG_FILL_REQUIRED_INFORMATION);
                    errorWindow.ShowDialog();
                }
            }

        }

        private void loadCarList()
        {
            DoctorCarService carService = new DoctorCarService();
            carList = carService.viewCarList();
            lsvCar.Items.Clear();
            foreach (tbl_car car in carList)
            {
                lsvCar.Items.Add(new ListViewItem { Content = car.REG_NO });
            }
        }

        private Boolean validateCar()
        {
            if (txtCarRegNo.Text == "" || txtPassword.Password == "" || txtConfPassword.Password == "")
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        private void btnCarCancel_Click(object sender, RoutedEventArgs e)
        {
            clearCarSettings();
        }

        private void clearCarSettings()
        {
            txtCarModel.Text = "";
            txtCarRegNo.Text = "";
            txtPassword.Password = "";
            txtConfPassword.Password = "";
            txtCarRemark.Text = "";
        }

        private void btnCarAddNew_Click(object sender, RoutedEventArgs e)
        {
            clearCarSettings();
            carViewChangeStatus("save");
        }

        private void carViewChangeStatus(string type)
        {
            if (type.Equals("save"))
            {
                spCarDetails.Visibility = System.Windows.Visibility.Visible;
                grdCarSave.Visibility = System.Windows.Visibility.Visible;
                grdCarUpdate.Visibility = System.Windows.Visibility.Hidden;
                txtCarRegNo.IsEnabled = true;
                lsvCar.SelectedIndex = -1;
                clearCarSettings();
            }
            else if (type.Equals("update"))
            {
                spCarDetails.Visibility = System.Windows.Visibility.Visible;
                grdCarSave.Visibility = System.Windows.Visibility.Hidden;
                grdCarUpdate.Visibility = System.Windows.Visibility.Visible;
                txtCarRegNo.IsEnabled = false;
            }

        }

        private void btnCarUpdate_Click(object sender, RoutedEventArgs e)
        {

            if (txtPassword.Password != txtConfPassword.Password)
            {
                MSGWindow errorWindow = new MSGWindow(Messages.TTL_PASS_MATCH, Messages.MSG_PASS_MATCH_ERROR);
                errorWindow.ShowDialog();
            }
            else
            {
                if (validateCar() == true)
                {
                    selectedCarObject.MODEL = txtCarModel.Text;
                    //selectedCarObject.REG_NO = txtCarRegNo.Text;
                    selectedCarObject.REMARK = txtCarRemark.Text;
                    selectedCarObject.USER_NAME = txtCarRegNo.Text;
                    selectedCarObject.PASSWORD = txtPassword.Password;

                    DoctorCarService carService = new DoctorCarService();



                    int status = carService.update(selectedCarObject);
                    if (status > 0)
                    {
                        MSGWindow success = new MSGWindow(Messages.TTL_CAR_ADD, Messages.MSG_CAR_UPDATE);
                        success.ShowDialog();
                        loadCarList();
                        clearCarSettings();
                    }
                }


                else
                {
                    MSGWindow errorWindow = new MSGWindow(Messages.TTL_FILL_REQUIRED, Messages.MSG_FILL_REQUIRED_INFORMATION);
                    errorWindow.ShowDialog();
                }
            }
        }

        private void btnCarDelete_Click(object sender, RoutedEventArgs e)
        {
            //MessageBoxResult messageBoxResult = System.Windows.MessageBox.Show("Are you sure to delete record ?", Messages.TTL_CAR_ADD, System.Windows.MessageBoxButton.YesNo);

            ConfirmationWindow messageBoxResult = new ConfirmationWindow(Messages.TTL_CAR_ADD, "Are you sure to delete record ?");
            messageBoxResult.ShowDialog();
            if (ConfirmationWindow.isConfirm == MessageBoxResult.Yes)
            {
                DoctorCarService carService = new DoctorCarService();
                int status = carService.remove(selectedCarObject);
                if (status > 0)
                {
                    MSGWindow success = new MSGWindow(Messages.TTL_CAR_ADD, Messages.MSG_CAR_DELETE);
                    success.ShowDialog();
                    loadCarList();
                    clearCarSettings();
                }
            }
        }

        private void lsvCar_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lsvCar.SelectedIndex != -1)
            {
                ListViewItem lvi = ((sender as ListView).SelectedItem as ListViewItem);
                foreach (tbl_car car in carList)
                {
                    if (car.REG_NO.Equals(lvi.Content.ToString()))
                    {
                        txtCarRegNo.Text = car.REG_NO;
                        txtCarModel.Text = car.MODEL;
                        txtCarRemark.Text = car.REMARK;
                        txtPassword.Password = car.PASSWORD;
                        txtConfPassword.Password = car.PASSWORD;

                        selectedCarObject = car;
                        carViewChangeStatus("update");
                    }
                }
            }
            else
            {
                carViewChangeStatus("save");
            }
        }

        /* End car settings */
    }
}
